const xml = `
<ul class="nested-sublink">
  <li class="sub-expand">
    <a href="/alphabet/lesson1">Lesson 1</a>
    <ul class="deep-sublink">
      <li>
        <a href="/alphabet/lesson1/p/">p</a>
      </li>
      <li>
        <a href="/alphabet/lesson1/b/">b</a>
      </li>
      <li>
        <a href="/alphabet/lesson1/m/">m</a>
      </li>
      <li>
        <a href="/alphabet/lesson1/t/">t</a>
      </li>
      <li>
        <a href="/alphabet/lesson1/d/">d</a>
      </li>
      <li>
        <a href="/alphabet/lesson1/n/">n</a>
      </li>
    </ul>
  </li>
  <li class="sub-expand">
    <a href="/alphabet/lesson2">Lesson 2</a>
    <ul class="deep-sublink">
      <li>
        <a href="/alphabet/lesson2/a/">a</a>
      </li>
      <li>
        <a href="/alphabet/lesson2/e/">e</a>
      </li>
      <li>
        <a href="/alphabet/lesson2/i/">i</a>
      </li>
      <li>
        <a href="/alphabet/lesson2/o/">o</a>
      </li>
      <li>
        <a href="/alphabet/lesson2/u/">u</a>
      </li>
    </ul>
  </li>
  <li class="sub-expand">
    <a href="/alphabet/lesson3">Lesson 3</a>
    <ul class="deep-sublink">
      <li>
        <a href="/alphabet/lesson3/k/">k</a>
      </li>
      <li>
        <a href="/alphabet/lesson3/g/">g</a>
      </li>
      <li>
        <a href="/alphabet/lesson3/ng/">ng</a>
      </li>
      <li>
        <a href="/alphabet/lesson3/f/">f</a>
      </li>
      <li>
        <a href="/alphabet/lesson3/v/">v</a>
      </li>
      <li>
        <a href="/alphabet/lesson3/s/">s</a>
      </li>
    </ul>
  </li>
  <li class="sub-expand">
    <a href="/alphabet/lesson4">Lesson 4</a>
    <ul class="deep-sublink">
      <li>
        <a href="/alphabet/lesson4/a/">a</a>
      </li>
      <li>
        <a href="/alphabet/lesson4/e/">e</a>
      </li>
      <li>
        <a href="/alphabet/lesson4/i/">i</a>
      </li>
      <li>
        <a href="/alphabet/lesson4/o/">o</a>
      </li>
      <li>
        <a href="/alphabet/lesson4/u/">u</a>
      </li>
    </ul>
  </li>
  <li class="sub-expand">
    <a href="/alphabet/lesson5">Lesson 5</a>
    <ul class="deep-sublink">
      <li>
        <a href="/alphabet/lesson5/z/">z</a>
      </li>
      <li>
        <a href="/alphabet/lesson5/th/">
          th <span>(voiced)</span>
        </a>
      </li>
      <li>
        <a href="/alphabet/lesson5/th-unvoiced/">
          th <span>(unvoiced)</span>
        </a>
      </li>
      <li>
        <a href="/alphabet/lesson5/sh/">sh</a>
      </li>
      <li>
        <a href="/alphabet/lesson5/zh/">zh</a>
      </li>
      <li>
        <a href="/alphabet/lesson5/ch/">ch</a>
      </li>
      <li>
        <a href="/alphabet/lesson5/j/">j</a>
      </li>
    </ul>
  </li>
  <li class="sub-expand">
    <a href="/alphabet/lesson6">Lesson 6</a>
    <ul class="deep-sublink">
      <li>
        <a href="/alphabet/lesson6/l/">l</a>
      </li>
      <li>
        <a href="/alphabet/lesson6/r/">r</a>
      </li>
      <li>
        <a href="/alphabet/lesson6/y/">y</a>
      </li>
      <li>
        <a href="/alphabet/lesson6/w/">w</a>
      </li>
      <li>
        <a href="/alphabet/lesson6/wh/">wh</a>
      </li>
      <li>
        <a href="/alphabet/lesson6/h/">h</a>
      </li>
    </ul>
  </li>
  <li class="sub-expand">
    <a href="/alphabet/lesson7">Lesson 7</a>
    <ul class="deep-sublink">
      <li>
        <a href="/alphabet/lesson7/aw/">aw</a>
      </li>
      <li>
        <a href="/alphabet/lesson7/oo/">oo</a>
      </li>
      <li>
        <a href="/alphabet/lesson7/oi/">oi</a>
      </li>
      <li>
        <a href="/alphabet/lesson7/ou/">ou</a>
      </li>
      <li>
        <a href="/alphabet/lesson7/er/">er</a>
      </li>
      <li>
        <a href="/alphabet/lesson7/ar/">ar</a>
      </li>
      <li>
        <a href="/alphabet/lesson7/or/">or</a>
      </li>
    </ul>
  </li>
</ul>

`;
const { JSDOM } = require("jsdom");
function getAllLessons() {
  // 将xml解析未dom
  const dom = new JSDOM(xml);
  const document = dom.window.document;
  // 遍历提取class='sub-expand'元素的第一个a元素提取出来作为groupName，a的href字符串中的数字作为groupID
  const groups = Array.from(document.querySelectorAll(".sub-expand")).map(el => {
    const a = el.querySelector("a");
    return {
      groupName: a.textContent,
      groupID: a.href.replace(/\D/g, ""),
    };
  });
  // 遍历提取class='sub-expand'元素的全部[li a],获取innerText作为lessonName
  const lessons = Array.from(document.querySelectorAll(".sub-expand li a")).map(el => {
    return {
      lessonName: el.textContent.replaceAll(/ /g, "").replaceAll(/\n/g, "").replace('(','_').replace(')',''),
      groupID: el.href.replace(/\D/g, ""),
    };
  });
  console.log(groups);
  return [groups, lessons];
}

// getAllLessons();

module.exports= getAllLessons;
